// 测试管理后台API接口

const testAdminApi = async () => {
    console.log('开始测试管理后台API接口...\n');
    
    // 测试数据
    const baseUrl = 'http://localhost:8000/api/v1/admin';
    const authToken = 'Bearer admin_token'; // 模拟token，实际使用需要登录获取
    
    const testCases = [
        {
            name: '接口11: 发布文章',
            url: `${baseUrl}/article/publish`,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': authToken
            },
            data: {
                title: '测试文章标题',
                content: '这是测试文章的内容...',
                intro: '这是文章简介',
                author: '测试作者',
                categoryId: 1,
                cover: 'test-cover.jpg'
            }
        },
        {
            name: '接口13: 编辑文章',
            url: `${baseUrl}/article/edit`,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': authToken
            },
            data: {
                id: 1,
                title: '编辑后的文章标题',
                content: '这是编辑后的文章内容...',
                intro: '这是编辑后的文章简介',
                author: '编辑后的作者',
                categoryId: 1,
                cover: 'updated-cover.jpg'
            }
        },
        {
            name: '接口14: 删除文章',
            url: `${baseUrl}/article/delete`,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': authToken
            },
            data: {
                id: 1
            }
        },
        {
            name: '接口15: 获取统计数据',
            url: `${baseUrl}/data/info`,
            method: 'GET',
            headers: {
                'Authorization': authToken
            }
        },
        {
            name: '接口16: 日新增文章数量统计',
            url: `${baseUrl}/data/article`,
            method: 'GET',
            headers: {
                'Authorization': authToken
            }
        }
    ];
    
    // 执行测试
    for (let testCase of testCases) {
        try {
            console.log(`\n🔥 测试 ${testCase.name}:`);
            console.log(`URL: ${testCase.method} ${testCase.url}`);
            
            const options = {
                method: testCase.method,
                headers: testCase.headers
            };
            
            if (testCase.data) {
                options.body = JSON.stringify(testCase.data);
            }
            
            const response = await fetch(testCase.url, options);
            const result = await response.text();
            
            console.log(`状态码: ${response.status}`);
            console.log(`响应: ${result}`);
            
            if (response.status === 200) {
                console.log('✅ 测试通过');
            } else {
                console.log(`❌ 测试失败 (状态码: ${response.status})`);
            }
            
        } catch (error) {
            console.log(`❌ 请求失败: ${error.message}`);
        }
    }
    
    console.log('\n测试完成！');
};

// 如果在Node.js环境下运行
if (typeof fetch === 'undefined') {
    console.log('请在浏览器环境中运行此测试脚本');
    console.log('或者安装node-fetch: npm install node-fetch');
} else {
    testAdminApi();
}
